package start.aai.controller;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;
import start.aai.common.Result;
import start.aai.entity.DTO.MessageDTO;
import start.aai.entity.Messages;
import start.aai.service.MessagesService;

import java.util.List;

@RestController
@RequestMapping("/ai/messages")
@Slf4j
public class MessagesController {
    @Resource
    private MessagesService messagesService;

    // 核心修复：直接接收文本+单文件参数
    @PostMapping(value = "/sendmessage", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> sendMessage(
            @RequestParam(required = false) String text,
            @RequestParam(required = false) MultipartFile file,
            @RequestParam Integer userId
    ) {
        return messagesService.chatStream(text, file,userId)
                .onErrorResume(e ->
                        Flux.just("event: error\ndata: " + e.getMessage() + "\n\n")
                );
    }

    @GetMapping("/getHistory")
    public Result getHistory(@RequestParam Integer conversationId) {
        List<Messages> list= messagesService.getHistory(conversationId);
        return Result.success(list);
    }
}